(function (){
    var host = getHost();
    function getHost(){
        var path = window.location.origin + window.location.pathname;
        var pathSz = path.split("/");
        pathSz.length = pathSz.length-5;
        path = pathSz.join("/");
        return path;
    }
    function setAjaxHost(){
        var path = window.location.origin + window.location.pathname;
        var pathSz = path.split("/");
        pathSz.length = pathSz.length-3;
        path = pathSz.join("/");
        utils.ajaxHost = path;
    }
    function zw(){
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext("2d");
        ctx.font = "24px Arial";
        ctx.fillText("carnoc",22,33);
        ctx.moveTo(0,0);
        ctx.lineTo(100,100);
        ctx.stroke();
        return utils.md5(canvas.toDataURL());
    }
    setAjaxHost();
    window.thirdSdk={
        getController:function(fn){
            var that = this;
            if(that.controllerUrl){
                fn(that.controllerUrl);
                return;
            }
            var cacheUrl = utils.getLocalStorage("controllerUrl");
            if(cacheUrl && cacheUrl.time>new Date().getTime()-1000*60*5){
                that.controllerUrl = cacheUrl.url;
                fn(that.controllerUrl);
                return;
            }
            utils.$.get("/php/action.php?type=0",function (res){
                if(res.flag && res.data.aliyun == "me"){
                    that.controllerUrl = utils.ajaxHost + "/editer-service/aliyundrive.php";
                }else{
                    that.controllerUrl = "https://hm50134.j.h57.site/editer-service/aliyundrive.php";
                }
                utils.$.get(that.controllerUrl+"?type=4",function (res){},true);
                utils.setLocalStorage("controllerUrl",{time:new Date().getTime(),url:that.controllerUrl});
                fn(that.controllerUrl);
            },true);
        },
        getConfig:function(fn){
            var config = {
                sv:5, //每隔多少秒执行一次保存
                save:false, //是否开启自动保存
                canEdit:true //是否可以编辑
            };
            utils.$.get("/php/action.php?type=0",function (res){
                if(res.flag){
                    config = res.data;
                }
                config.canEdit = true;
                if(!utils.getParamer("parentPath")){
                    config.canEdit = false;
                }
                fn(config);
            },true);
        },
        getAliFileId:function (options,fn){
            //获取阿里云文件id
            this.getController(function(ac){
                utils.$.post(ac+"?type=1",options,function (res){
                    fn(res.data.file_id);
                    utils.$.post("/php/action.php?type=8",{path:utils.getParamer("path"),file_id:res.data.file_id},function (res){
                    },true);
                });
            });
        },
        getAliPreviewDataByFid:function(file_id,fn){
            //获取阿里云盘预览链接
            this.getController(function(ac){
                utils.$.post(ac+"?type=2",{file_id:file_id},function (res){
                    fn(res);
                },true);
            });
        },
        getAliEditDataByFid:function(file_id,fn){
            //获取阿里云盘编辑链接
            this.getController(function(ac){
                utils.$.post(ac+"?type=3",{file_id:file_id},function (res){
                    fn(res);
                },true);
            });
        },
        aliSaveFile:function(file_id,fn){
            //保存文件
            var name = utils.getParamer("name");
            this.getController(function(ac){
                var file_url = ac+"?type=5&file_id="+file_id+"&name="+encodeURIComponent(name);
                var parentPath = utils.getParamer("parentPath");
                utils.$.post("/php/action.php?type=2",{file_url:file_url,parentPath:parentPath,name:name},function (res){
                    fn(res);
                },true);
            });
        },
        getPersons:function(fn){
            //获取在线人数
            //后端自行实现,步骤如下
            //1.获取用户信息,并提交到分享人列表
            //2.下载分享人列表
            //3.使用fn进行回调 fn({flag:true,data:sz});
            var that = this;
            if(!that.llqZw){
                that.llqZw = zw();
            }
            utils.$.attr.tmpMask = false;
            utils.$.post("/php/action.php?type=5",{path:utils.getParamer("path"),zw:that.llqZw},function (res){
                fn(res);
            },true);
        },
        getDocConfig:function (password,fn){
            //获取文档配置
            //后端自行实现,步骤如下
            //1.根据文档地址或者文件id获取分享时候的配置
            //2.校验配置是否合法
            //3.使用fn进行回调 fn({flag:true,data:{needPass:false,checkPass:false,needLogin:false,checkLogin:true}});
            utils.$.post("/php/action.php?type=7",{path:utils.getParamer("path"),password:password},function (res){
                fn(res);
            },true);
        },
        toLogin:function (){
            //跳转到登录页面
            //自行实现,建议如下
            //1.打印提示用户未登录
            //2.用户点击后跳转到登录页
            utils.$.alert("当前文档必须登录后才能查看/编辑",function (){
                window.location.href = host;
            });
        },
        setDocConfig:function (config,fn){
            //设置文档配置
            //后端自行实现,步骤如下
            //1.将config存起来  config = {
            //                     needPass:false,
            //                     needLogin:false,
            //                     password:""
            //                 }
            //2.使用fn回调  fn({flag:true,msg:"设置文档成功"});
            utils.$.post("/php/action.php?type=3",{path:utils.getParamer("path"),config:config},function (res){
                fn(res);
            },true);
        },
        hasSetDocConfigAuth:function(fn){
            //是否有设置文档配置权限
            //后端自行实现,步骤如下
            //1.获取用户信息
            //2.校验此用户是否是文档分享发起人
            //3.使用fn回调  fn(false);
            if(!utils.getParamer("parentPath")){
                fn(false);
                return;
            }
            utils.$.attr.tmpMask = false;
            utils.$.post("/php/action.php?type=4",{path:utils.getParamer("path")},function (res){
                fn(res.flag);
            },true);

        },
        getShortUrl:function(url,fn){
            //获取短链接
            utils.$.post("/php/action.php?type=9",{path:utils.getParamer("path"),url:url},function (res){
                var shortUrl = utils.ajaxHost+"/static/short.html?path="+encodeURIComponent(utils.getParamer("path"));
                if(!res.flag){
                    shortUrl = url;
                }
                fn(shortUrl);
            },true);

        }

    }
})()